<form class="layui-form  seller-alone-form" id="bookRelAdd" style="padding: 20px;">

    <div class="layui-form-item">
        <input type="hidden" name="src_book_id" value="{$bookInfo.id}">
        <input type="hidden" name="plan_code" value="{$bookInfo.plan_code}">
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>年度：</label>
            <div class="layui-input-inline" id="year_box">
                <select name="plan_year" lay-filter="plan_year" lay-verify="required" id="plan_year" list="year_list" lay-search>
                    <option value=""></option>
                    <datalist  id="year_list" lay-search>
                        {volist name="yearList" id="yearList"}
                        <option value="{$yearList.id}" {eq name="$yearList.year" value="$year"}selected{/eq}>{$yearList.year}</option>
                        {/volist}
                    </datalist>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>季度：</label>
            <div class="layui-input-inline" id="season_box">
                <select name="plan_season" lay-filter="plan_season" lay-verify="required" id="plan_season" list="season_list" lay-search>
                    <option value=""></option>
                    <datalist  id="season_list" lay-search>
                        {volist name="seasonList" id="seasonList"}
                        <option value="{$seasonList.season_id}" {eq name="$seasonList.season_name" value="$season"}selected{/eq}>{$seasonList.season_name}</option>
                        {/volist}
                    </datalist>
                </select>
            </div>
        </div>
    </div> 

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>类型：</label>
            <div class="layui-input-inline">
                <select id="type_id" lay-filter="type_id" lay-verify="required" name="type_id">
                    <option value=''>请选择</option>
                    {volist name="typeList" id="vo"}
                    <option value="{$vo.id}">{$vo.name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>参数：</label>
            <div class="layui-input-inline">
                <select name="param_id" lay-filter="param_id" lay-verify="required" id="param_id" lay-search>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item"> 
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>教材：</label>
            <div class="layui-input-inline" style="width: 514px;">
                <select name="book_id" id="book_id" lay-filter="book_id" lay-verify="required" lay-search>
                    <option value=""></option>
                    {volist name='bookList' id='vo'}
                        <option value="{$vo.id}">{$vo.book_name}</option>
                    {/volist}
                </select>
            </div>
        </div>
    </div> 
    <div class="layui-form-item"> 
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>主编：</label>
            <div class="layui-input-inline">
                <input type="text" name="book_author" id="book_author" lay-verify="required" style="color:#C0C4CC" disabled="true" Readonly="true" class="layui-input" value="">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>出版社：</label>
            <div class="layui-input-inline">
                <input type="text" name="book_concern" id="book_concern" lay-verify="required" style="color:#C0C4CC" disabled="true" Readonly="true" class="layui-input" value="">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>单价：</label>
            <div class="layui-input-inline">
                <input type="text" name="price" id="price" class="layui-input" disabled="true" style="color:#C0C4CC" Readonly="true" value="">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>教材数量：</label>
            <div class="layui-input-inline">
                <input type="text" name="book_num" class="layui-input" lay-verify="required" value="">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>折扣：</label>
            <div class="layui-input-inline">
                <input type="text" name="book_disc" id="book_disc" class="layui-input" disabled="true" style="color:#C0C4CC" Readonly="true" value="">
            </div>
        </div>
    </div>    
    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="hidden" name="open_spec" value="0">
            {:zlserverToken()}
            <button id="book_rel_add_save" class="layui-btn" lay-submit="" lay-filter="save">保存</button>
        </div>
    </div>
    {:zlserverToken()}
</div>
<script>
    layui.extend({
        dtree: '__STATIC_LIB__layuiadmin/layui/layui_ext/dtree/dtree'
    }).use(['form', 'laydate', 'dtree'], function () { 
        var form = layui.form;
  
        //复制计划
        form.on('submit(save)', function (data) {
            formData = data.field;
            if (!formData) {
                layer.msg('请先完善数据', { time: 1300 });
                return false;
            }

            // 判断单价：
            // if(formData.price <= 0) {
            //     layer.msg('请输入合法的单价', { time: 1300 });
            //     return false;
            // }
            if(formData.book_num <= 0) {
                layer.msg('请输入合法的数量', { time: 1300 });
                return false;
            }

            $("#book_rel_add_save").addClass("layui-btn-disabled");
            $('#book_rel_add_save').attr("disabled", true);

            $.ajax({
                type: 'post',
                url: '{:url("BookDraw/BookExtraDrawBookRelAdd")}',
                data: formData,
                dataType: 'json',
                success: function (e) {
                    if (e.status === true) {
                        layer.open({
                            type: 1
                            , offset: 'auto'
                            , id: 'layerDemoAuto'
                            , content: '<div style="padding: 20px 100px;">' + e.msg + '</div>'
                            , btn: '关闭'
                            , btnAlign: 'c' //按钮居中
                            , shade: 0.3 //不显示遮罩
                            , yes: function () {
                                // 设置table请求参数（当操作的年度季度与刷新目标窗口不一致时）
                                var where = {
                                    year : formData.plan_year,
                                    season : formData.plan_season
                                }

                                // 设置筛选条件（当操作的年度季度与刷新目标窗口不一致时）
                                var control = {
                                    '#year': formData.plan_year,
                                    '#season': formData.plan_season
                                }

                                parent.layui.admin.events.changeTabAndRefresh('/manage/book_draw/bookextradrawbookrel.html', 'bookRelTable', "{:url('BookDraw/BookExtraDrawBookRel')}?year="+formData.plan_year+"&season="+formData.plan_season+"&type_id=&param_id=", true, where, control);
                            }, close: function () {
                                console.log('close');
                            }, end: function () {
                                // 设置table请求参数（当操作的年度季度与刷新目标窗口不一致时）
                                var where = {
                                    year : formData.plan_year,
                                    season : formData.plan_season
                                }

                                // 设置筛选条件（当操作的年度季度与刷新目标窗口不一致时）
                                var control = {
                                    '#year': formData.plan_year,
                                    '#season': formData.plan_season
                                }

                                parent.layui.admin.events.changeTabAndRefresh('/manage/book_draw/bookextradrawbookrel.html', 'bookRelTable', "{:url('BookDraw/BookExtraDrawBookRel')}?year="+formData.plan_year+"&season="+formData.plan_season+"&type_id=&param_id=", true, where, control);
                            }

                        }); 
                    } else {
                        $("#book_rel_add_save").removeClass("layui-btn-disabled");
                        $('#book_rel_add_save').removeAttr("disabled");

                        layer.msg(e.msg, { time: 1300 });
                    }
                }
            });
            return false;
        });

        layui.use('form', function () {
            // 联动参数
            form.on('select(type_id)', function (data) {
                getParams(data);
            });

            // 联动图书
            form.on('select(book_id)', function (data) {
                getBookInfo(data);
            });
            
        });
        
        // 得到参数
        function getParams(data) {
            $.ajax({
                type: 'get',
                url: "{:url('BookCommon/getParams')}",
                data: 'type_id=' + data.value,
                success: function(e) {
                    if(e.status) {
                        $("#param_id").empty();//清空下拉框的值
                        $('#param_id').append(new Option('', ''));// 下拉菜单里添加元素 
                        $.each(e.data, function (index, item) {
                            $('#param_id').append(new Option(item.name, item.id));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    } else {
                        layer.msg(e.msg);
                    }
                }
            });
        };

        // 得到教材信息
        function getBookInfo(data) {
            $.ajax({
                type: 'post',
                url: "{:url('Book/getBookInfo')}",
                data: 'id=' + data.value,
                success: function(e) {
                    if(e.status) {
                        $("#book_author").val(e.data.book_author);
                        $("#book_concern").val(e.data.book_concern);
                        $("#price").val(e.data.book_price);
                        $("#book_disc").val(e.data.book_disc);
                    } else {
                        layer.msg(e.msg);
                    }
                }
            });
        };
        
    });
</script>